package cn.edu.besti.cs1723.Z2316;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;

public class HuffmanCode
{
    public static void main(String[] args) throws IOException {
        File file = new File("C:\\Users\\赵乾宸\\IdeaProjects\\20172316_de_hello-world\\src\\cn\\edu\\besti\\cs1723\\Z2316\\abc.txt");

        Scanner scan = new Scanner(file);
        String s = scan.nextLine();
        System.out.println(s);
        int[] array = new int[28];
        for (int i = 0; i < array.length; i++) {
            array[i] = 0;
        }
        for (int i = 0; i < s.length(); i++) {
            char x = s.charAt(i);
            if (x - 'a' == -65)
                array[26]++;
            else if (x - 'a' == -53)
                array[27]++;
            else
                array[x - 'a']++;
        }
        System.out.println("字母频率：");
        for (int i = 0; i < array.length - 2; i++) {
            System.out.print((char)('a'+ i)+":"+(double) array[i] / s.length()+" ");
        }
        System.out.print("空格:" + (double) array[26] / s.length());
        System.out.println("逗号:" + (double) array[27] / s.length());

        HuffmanTreeNode[] huffmanTreeNodes = new HuffmanTreeNode[array.length];
        for (int i = 0; i < array.length; i++) {
            huffmanTreeNodes[i] = new HuffmanTreeNode(array[i], (char) ('a' + i), null, null, null);
        }

        HuffmanTree huffmanTree = new HuffmanTree(huffmanTreeNodes);

        System.out.println("字母编码:");
        String[] codes = huffmanTree.getEncoded();
        for (int i = 0; i < codes.length - 2; i++) {
            System.out.println((char) ('a' + i) + ":" + codes[i]);
        }
        System.out.println("  :" + codes[26]);
        System.out.println(", :" + codes[27]);

        String result = "";
        for (int i = 0; i < s.length(); i++) {
            int x = s.charAt(i) - 'a';
            if (x == -65)
                x = 26;
            if (x == -53)
                x = 27;
            result += codes[x];
        }
        System.out.println("加密：" + result);
        File file1 = new File("C:\\Users\\赵乾宸\\IdeaProjects\\20172316_de_hello-world\\src\\cn\\edu\\besti\\cs1723\\Z2316\\code.txt");
        FileWriter fileWriter = new FileWriter(file1);
        fileWriter.write(result);
        fileWriter.close();

        Scanner scan1 = new Scanner(file1);
        String s1 = scan1.nextLine();
        HuffmanTreeNode huffmanTreeNode = huffmanTree.getRoot();
        String result2 = "";
        for (int i = 0; i < s1.length(); i++) {
            if (s1.charAt(i) == '0') {
                if (huffmanTreeNode.getLeft() != null) {
                    huffmanTreeNode = huffmanTreeNode.getLeft();
                }
            } else {
                if (s1.charAt(i) == '1') {
                    if (huffmanTreeNode.getRight() != null) {
                        huffmanTreeNode = huffmanTreeNode.getRight();
                    }
                }
            }
            if (huffmanTreeNode.getLeft() == null && huffmanTreeNode.getRight() == null) {
                result2 += huffmanTreeNode.getElement();
                huffmanTreeNode = huffmanTree.getRoot();
            }

            result2 = result2.replace("{", " ");
            result2 = result2.replace("|", ",");
        }
        System.out.println("解密：" + result2);
        File file2 = new File("C:\\Users\\赵乾宸\\IdeaProjects\\20172316_de_hello-world\\src\\cn\\edu\\besti\\cs1723\\Z2316\\decode.txt");
        FileWriter fileWriter1 = new FileWriter(file2);
        fileWriter1.write(result2);

        System.out.println("编码解码后原来是否一致："+result2.equals(s));

    }
}
